import { fileURLToPath, URL } from 'node:url';

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueDevTools from 'vite-plugin-vue-devtools';

import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import Icons from 'unplugin-icons/vite';
import IconsResolver from 'unplugin-icons/resolver';

import UnoCSS from 'unocss/vite';

import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';

// https://vite.dev/config/
export default defineConfig({
    plugins: [
        vue(),
        vueDevTools(),
        AutoImport({
            imports: ['vue', 'vue-router', 'pinia'],
            resolvers: [ElementPlusResolver()],
        }),
        Components({
            resolvers: [
                IconsResolver(),
                ElementPlusResolver(),
            ],
        }),
        Icons({
            compiler: 'vue3',
            autoInstall: true,
            scale: 1.2,
            defaultStyle: '',
            defaultClass: '',
        }),
        UnoCSS(),
    ],
    resolve: {
        alias: {
            '@': fileURLToPath(new URL('./src', import.meta.url)),
        },
    },
    server: {
        proxy: {
            // 代理配置
            '/api': {
                target: 'http://localhost:8080', // 后端服务器地址
                changeOrigin: true, // 允许跨域
                rewrite: (path) => path.replace(/^\/api/, ''), // 重写路径，去掉 /api 前缀
            },
        },
    },
});